package com.controller.backend;

import com.biz.MenuRightBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.ManagerInfo;
import com.entity.model.MenuRight;
import com.util.SystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by asan on 2017/09/17.
 */
@Controller
@RequestMapping("/backend/security/menuRight")
public class MenuRightController extends BaseController<MenuRight> {
    @Autowired
    private MenuRightBiz menuRightBiz;

    @RequestMapping(value = "delMr")
    @ResponseBody
    public Result delMr(MenuRight menuRight) {
        if (menuRight != null) {
            Condition cond = new Condition(MenuRight.class);
            cond.createCriteria().andEqualTo("managerId", menuRight.getManagerId());
            menuRightBiz.deleteByCond(cond);
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genFailResult("操作失败");
    }


    @RequestMapping(value = "btnRight")
    @ResponseBody
    public Result btnRight(Integer menuId, HttpSession session) {
        if (menuId != null) {
            ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
            if (managerInfo.getId() == 1) {
                return ResultGenerator.genSuccessResult();
            } else {
                List<MenuRight> list = menuRightBiz.findBtnRight(menuId, managerInfo.getId());
                if (list.size() < 1) {
                    return ResultGenerator.genFailResult("非法操作");
                }
                return ResultGenerator.genSuccessResult(list);
            }
        }
        return ResultGenerator.genFailResult("非法操作");
    }
}
